﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Group_73
{
    public abstract class VisibleGameObject
    {
        #region Thuộc tính
        private bool enable;

        protected bool Enable
        {
            get { return enable; }
            set { enable = value; }
        }

        protected bool flipped = false;

        private Color ogirinalColor = Color.White;

        protected Color OgirinalColor
        {
            get { return ogirinalColor; }
            set { ogirinalColor = value; }
        }
        private Color affectColor = Color.White;

        protected Color AffterColor
        {
            get { return affectColor; }
            set { affectColor = value; }
        }

        private float scale = 1f;

        protected float Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        protected float drawDepth = 0.85f;

        private Vector2 worldLocation;

        protected Vector2 WorldLocation
        {
            get { return worldLocation; }
            set { worldLocation = value; }
        }

        protected Vector2 velocity;

        protected string currentAnimation;

        protected Dictionary<string, Animation> animations = new Dictionary<string, Animation>();
        #endregion
       
        public int FrameHeight
        {
            get
            { return (int)(animations[currentAnimation].FrameHeight * Scale); }

        }

        public int FrameWidth
        {
            get { return (int)(animations[currentAnimation].FrameWidth * Scale);}
        }

        public Vector2 WordCenter
        {
            get
            {
                return new Vector2(worldLocation.X - animations[currentAnimation].FrameWidth /2,
                                    worldLocation.Y - animations[currentAnimation ].FrameHeight/2);
            }
        }

        protected virtual Point offset()
        {
            return new Point(animations[currentAnimation].FrameWidth / 2, animations[currentAnimation].FrameHeight / 2);
        }

        public Rectangle WordRectangle
        {
            get
            {
                return new Rectangle(
                    (int)(worldLocation.X - offset().X),
                    (int)(worldLocation.Y - offset().Y),
                    animations[currentAnimation].FrameWidth,
                    animations[currentAnimation].FrameHeight);
            }
        }

        //Vùng đụng độ
        public Rectangle CollisionRectangle
        {
            get 
            {
                return new Rectangle(
                      (int)(worldLocation.X - scale * offset().X),
                      (int)(worldLocation.Y - scale * offset().Y),
                      (int)scale * animations[currentAnimation].FrameWidth,
                      (int)scale * animations[currentAnimation].FrameHeight);
                
            }
        }

        private void UpdateAnimation(GameTime gameTime)
        {
            if (animations.ContainsKey(currentAnimation))
            {
                if (animations[currentAnimation].FinishedPlaying)
                {
                    PlayAnimation(animations[currentAnimation].NextAnimation);
                }
                else
                {
                    animations[currentAnimation].Update(gameTime);
                }
            }
        }

        public void PlayAnimation(string name)
        {
            if (name != null && animations.ContainsKey(name))
            {
                currentAnimation = name;
                animations[name].play();
            }
        }

        public void PlayAnimation(string name, bool waitFinish,bool loop,bool reset)
        {
            if (reset || name != currentAnimation)
            {
                if (waitFinish && !animations[currentAnimation].FinishedPlaying)
                    return;

                currentAnimation = name;
                animations[currentAnimation].LoopAnimation = loop;
                animations[currentAnimation].play();

            }
        }

        public bool IsCollideWith(VisibleGameObject obj, bool originLogic)
        {
            if (originLogic)
                return CollisionRectangle.Intersects(obj.CollisionRectangle) && Math.Abs(worldLocation.Y - obj.worldLocation.Y) <= 10;
            return CollisionRectangle.Intersects(obj.CollisionRectangle);
        }

        public virtual void Update(GameTime gameTime)
        {
            if (enable == false)
                return;

            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            UpdateAnimation(gameTime);

            Vector2 moveAmout = velocity * elapsed;
            Vector2 newPosition = worldLocation + moveAmout;

            newPosition = new Vector2(
                MathHelper.Clamp(newPosition.X, 0, Camera.WorldRectangle.Width),
                MathHelper.Clamp(newPosition.Y, 0, Camera.WorldRectangle.Height));

            worldLocation = newPosition;
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if (enable == false)
                return;

            if (animations.ContainsKey(currentAnimation))
            {
                SpriteEffects effect = SpriteEffects.None;
                if(flipped)
                    effect = SpriteEffects.FlipHorizontally;

                Vector2 og = new Vector2(animations[currentAnimation].FrameWidth / 2, animations[currentAnimation].FrameHeight);
                spriteBatch.Draw(
                    animations[currentAnimation].Texture,
                    Camera.WorldToScreen(WorldLocation),
                    animations[currentAnimation].FrameRectangle,
                    affectColor, 0f, og, scale, effect, drawDepth);
              
            }
        }
    }
}
